package com.xtx.air.bo.operator.impl;

import com.xtx.air.bo.operator.IOperatorBO;
import com.xtx.air.dao.feature.IFeatureDAO;
import com.xtx.air.dao.operator.IOperatorDAO;
import com.xtx.air.dataobject.OperatorDO;
import com.xtx.exception.BOException;
import com.xtx.util.RegularUtil;
import com.xtx.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.util.ByteSource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * User: hanyi
 * Date: 12-2-7
 * Time: 下午11:51
 */
public class OperatorBOImpl implements IOperatorBO {
    private Log logger = LogFactory.getLog(this.getClass());
    public IOperatorDAO operatorDAO;
    public IFeatureDAO featureDAO;

    public void setOperatorDAO(IOperatorDAO operatorDAO) {
        this.operatorDAO = operatorDAO;
    }

    public void setFeatureDAO(IFeatureDAO featureDAO) {
        this.featureDAO = featureDAO;
    }

    @Override
    public List<String> findOperatorPermission(String operName) {
        try {
            List<String> roles = operatorDAO.findOperatorRole(operName);
            List<String> permissions = new ArrayList<String>();
            for (String role : roles) {
                permissions.addAll(featureDAO.findOperatorPermissionByRole(role));
            }
            return permissions;
        } catch (Exception e) {
            logger.error("查询用户权限失败,operName=" + operName, e);
            return Collections.EMPTY_LIST;
        }
    }

    @Override
    public void addOperator(OperatorDO operator) {
        if (StringUtil.isEmpty(operator.getOperLogName())) {
            throw new IllegalArgumentException("操作员名称不能为空");
        }
        if (StringUtil.isEmpty(operator.getOperName())) {
            throw new IllegalArgumentException("操作员真实姓名不能为空");
        }
        if (StringUtil.isEmpty(operator.getOperLogPwd())) {
            throw new IllegalArgumentException("密码不能为空");
        }
        if (!RegularUtil.isEmail(operator.getEmail())) {
            throw new IllegalArgumentException("email不符合格式");
        }
        if (operator.getPhone() == null) {
            throw new IllegalArgumentException("号码不能为空");
        }
        Integer count = operatorDAO.getOperatorCountByName(operator.getOperLogName());
        if (count != null && count > 0) {
            throw new IllegalArgumentException("当前用户已经存在");
        }
        RandomNumberGenerator rng = new SecureRandomNumberGenerator();
        ByteSource salt = rng.nextBytes();
        String hashedPasswordBase64 = new Sha256Hash(operator.getOperLogPwd(), salt, 1024).toBase64();
        operator.setOperLogPwd(hashedPasswordBase64);
        operator.setSalt(salt.toBase64());
        try{
            operatorDAO.addOperator(operator);
        }catch (Exception e){
            logger.error("新增操作员出错",e);
            throw new BOException(e);
        }
    }
}
